home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: in2.uu.net!allegra!alice!bs
- From: bs@research.att.com (Bjarne Stroustrup <9758-26353> 0112760)
- Subject: Re: Smart Pointer Implementation & question
- Message-ID: <DLDw3H.7L1@research.att.com>
- Organization: Info. Sci. Div., AT&T Bell Laboratories, Murray Hill, NJ
- References: <4dfe36$d99@grid.direct.ca> <749@beech.ukc.ac.uk>
- Date: Thu, 18 Jan 1996 15:52:29 GMT
-
-
- Article 125614 of comp.lang.c++:
-
- From: R.E.Jones@ukc.ac.uk (rej) writes
-
- > In article <4dfe36$d99@grid.direct.ca>, Ken Clark <ken@direct.ca> wrote:
- > >Hi. I have implemented a reference counting smart pointer class. It works
- > >well, except that I can't get a basic behavior of normal pointers:
- > >automatic casting of subclass pointers.
- >
- > This is a well-known problem with smart pointers.
- >
- > A good survey of the extent of the problem and possible solutions can be
- > found in
- >
- > @inproceedings{edel92b,
- > author = "Daniel R. Edelson",
- > title = "Smart Pointers: They're Smart, But They're Not Pointers",
- > booktitle = "USENIX C++ Conference",
- > year = 1992
- > }
-
- However, things have progressed since that paper was written.
-
- The problem is discussed in sec15.9.1 of D&E. You need member templates.
- Here is the key example:
-
- template<class T> class Ptr { // pointer to T
- T* p;
- public:
- Ptr(T*);
- template<class T2> operator Ptr<T2> () {
- return Ptr<T2>(p); // works iff p can be
- // converted to a T2*
- }
- // ...
- };
-
- That way, a Ptr<D> will convert to a Ptr<B> iff a D* converts to B*.
-
- - Bjarne
-